﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;
using System.Data.OleDb;
using libConn;

namespace ApartmentSale
{
    public partial class ReceiptEdit : Window
    {
        public ReceiptEdit()
        {
            InitializeComponent();
            currentReceipt = new ReceiptView();
        }

        public ReceiptEdit(ReceiptView receipt)
            : this()
        {
            currentReceipt = receipt;

            if (receipt.receipt_code != "")
            {
                tbCode.Text = receipt.receipt_code;
                tbCode.IsEnabled = false;
                this.Title = "Modify receipt";
            }

            if (receipt.receipt_type == 0 || receipt.receipt_type == 1)
            {
                tbCash.IsEnabled = false;
                tbCard.IsEnabled = false;
                tbCheck.IsEnabled = false;

                this.Title += "- " + receipt.type_string + "Changes in the relevant receipts window";
            }

            lbType.Content = receipt.type_string;
            lbAmount.Content = String.Format("{0:N2}", receipt.receipt_amount);
            dpDate.SelectedDate = receipt.receipt_date;

            tbCash.Text = String.Format("{0:N2}", receipt.receipt_cash_amount);
            tbCard.Text = String.Format("{0:N2}", receipt.receipt_card_amount);
            tbCheck.Text = String.Format("{0:N2}", receipt.receipt_check_amount);
        }

        private ReceiptView currentReceipt;

        private void AmountChanged(object sender, TextChangedEventArgs e)
        {
            TextBox tb = sender as TextBox;
            if (tb != null)
            {
                double d;
                if (tb.Text == "" || !Double.TryParse(tb.Text, out d) || d < 0)
                {
                    tb.Text = "0.00";
                }
            }

            double cash, card, check;
            if (tbCash.Text == "" || !Double.TryParse(tbCash.Text, out cash) || cash < 0)
            {
                cash = 0;
            }

            if (tbCard.Text == "" || !Double.TryParse(tbCard.Text, out card) || cash < 0)
            {
                card = 0;
            }

            if (tbCheck.Text == "" || !Double.TryParse(tbCheck.Text, out check) || cash < 0)
            {
                check = 0;
            }

            currentReceipt.receipt_amount = cash + card + check;
            lbAmount.Content = String.Format("{0:N2}", currentReceipt.receipt_amount);
        }

        private bool checkReceiptCode(string data)
        {
            string errMessage = null;
            string str = "SELECT * FROM receipt WHERE receipt_code = '"
                + data.Replace("'", "''") + "'";

            DataTable oleDT;
            oleDT = libConn.AccdbConn.query(str, ref errMessage);

            if (errMessage != null)
            {
                System.Windows.MessageBox.Show(
                    errMessage,
                    "Check the receipt SN failed",
                    System.Windows.MessageBoxButton.OK,
                    System.Windows.MessageBoxImage.Error);
                return false;
            }

            if (oleDT.Rows.Count > 0)
            {
                System.Windows.MessageBox.Show(
                    "Exist in the same receipt SN, please check.",
                    "Save failed",
                    System.Windows.MessageBoxButton.OK,
                    System.Windows.MessageBoxImage.Stop);
                return false;
            }

            return true;
        }

        private void btOK_Click(object sender, RoutedEventArgs e)
        {
            if (tbCode.IsEnabled)
            {
                if (tbCode.Text == "")
                {
                    MessageBox.Show("Please enter the receipt SN.");
                    tbCode.Focus();
                    return;
                }

                if (!checkReceiptCode(tbCode.Text))
                {
                    tbCode.Focus();
                    return;
                }
            }

            if (dpDate.SelectedDate == null)
            {
                MessageBox.Show("Please enter the issue date of receipt.");
                dpDate.Focus();
                return;
            }
            currentReceipt.receipt_date = (DateTime)dpDate.SelectedDate;

            currentReceipt.receipt_code = tbCode.Text;
            currentReceipt.receipt_cash_amount = Double.Parse(tbCash.Text);
            currentReceipt.receipt_card_amount = Double.Parse(tbCard.Text); 
            currentReceipt.receipt_check_amount = Double.Parse(tbCheck.Text);

            currentReceipt.has_changed = true;
            this.Close();
        }

        private void btCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }


    }
}
